package com.example.a80205050.myapplication.MyActivity;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.icu.util.Calendar;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.a80205050.myapplication.Dao.InaccountDao;
import com.example.a80205050.myapplication.Modle.Tb_inaccount;

/**
 * Created by 80205050 on 2017/10/31.
 */
public class AddInaccount extends Activity {
    protected static final int DATE_DIALOG_ID=0;//创建气日期对话框常量
    EditText txtInMoney,txtInTime,txtInHandler,txtInMark;//四个编辑框的对象
    Spinner spInType;//类别的是spinner对象
    Button btnInSaveButton,btnInCancelButton;//取消和保存按钮
    private int mYear;//年
    private int mMonth;//月
    private int mDay;//日

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addinaccount);  //设置布局文件
        txtInMoney=(EditText)findViewById(R.id.txtInMoney);//获取输入金额的编辑框
        txtInTime=(EditText)findViewById(R.id.txtInTime);//获取输入时间的编辑框
        txtInHandler=(EditText)findViewById(R.id.txtInHandler);//获取付款方的编辑框
        txtInMark=(EditText)findViewById(R.id.txtInMark);//获取备注的编辑框
        btnInCancelButton=(Button)findViewById(R.id.btnInCancel); //获取取消的按钮
        btnInSaveButton=(Button)findViewById(R.id.btnInSave); //获取保存的按钮
        spInType=(Spinner)findViewById(R.id.spInType);

        txtInTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(DATE_DIALOG_ID);
            }
        });

        btnInSaveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String strInMoney=txtInMoney.getText().toString();//获取输入的金额
                if(!strInMoney.isEmpty()){
                    InaccountDao inaccountDao=new InaccountDao(AddInaccount.this);//获取数据库操作的对象
                    //生成收入的实体类对象
                    Tb_inaccount tb_inaccount=new Tb_inaccount(inaccountDao.getMaxId()+1,
                            Double.parseDouble(strInMoney),
                            txtInTime.getText().toString(),
                            spInType.getSelectedItem().toString(),
                            txtInHandler.getText().toString(),
                            txtInMark.getText().toString());
                    inaccountDao.add(tb_inaccount);//添加收入信息到数据库
                    Toast.makeText(AddInaccount.this,"新增收入数据添加到数据库成功！",Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(AddInaccount.this,"请输入收入金额",Toast.LENGTH_LONG).show();
                }

            }
        });

        btnInCancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                txtInMoney.setText(" ");//设置金额文本为空
                txtInMoney.setHint("0.00");//设置金额文本框的提示
                txtInTime.setText(" ");//设置时间文本框为空
                txtInTime.setHint("2010-01-01");//设置时间提示框的提示
                txtInHandler.setText(" ");//设置付款文本框为空
                txtInMark.setText(" ");//设置备注的文本框为空
                spInType.setSelection(0);//默认选择第一项
            }
        });
        final Calendar calendar=Calendar.getInstance();//获取系统当前的日期
        mYear=calendar.get(Calendar.YEAR);//获取年份
        mMonth=calendar.get(Calendar.MONTH);//获取月份
        mDay=calendar.get(Calendar.DATE);//获取天数
        updateDisplay();
    }
     private  DatePickerDialog.OnDateSetListener mDataSetListener=new DatePickerDialog.OnDateSetListener() {
         @Override
         public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
             mYear=i;
             mMonth=i1;
             mDay=i2;
         }
     };
    @Nullable
    @Override
    protected Dialog onCreateDialog(int id, Bundle args) {
        switch (id){
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this,mDataSetListener,mYear,mMonth,mDay);
        }
        return null;
    }
    //更新当前系统的中显示的时间
    private void updateDisplay(){
        txtInTime.setText(new StringBuilder().append(mYear)
        .append("-").append(mMonth+1).append("-").append(mDay));
    }
}
